
#include "nane/CodecSystem/Sound/SoundCodecSystem.h"
#include "nane/CodecSystem/Sound/SoundCodecSystemImpl_.h"
#include "nane/CodecSystem/Sound/SoundDecoderImpl_.h"

#include "nane/core/assert.h"

namespace nane
{
    ////////////////////////////////////////////////////////////////////////////////////////////////
    SoundCodecSystem::SoundCodecSystem()
        : SubSystem<ISoundCodecSystem>( new Implementation() )
        , m( static_cast<Implementation*>(SubSystem<ISoundCodecSystem>::m) )
    {
        
    }    
    ////////////////////////////////////////////////////////////////////////////////////////////////
    SoundCodecSystem::~SoundCodecSystem()
    {
        Shutdown();
        delete m;
        m = NULL;
    }    
    ////////////////////////////////////////////////////////////////////////////////////////////////
    const char* SoundCodecSystem::GetFileType()
    {
        NANE_ASSERT_RV( m->interface != NULL, "SoundCodecSystem must be initialized", "" );
        return m->interface->GetFileType();
    }    
    ////////////////////////////////////////////////////////////////////////////////////////////////
    SoundDecoder SoundCodecSystem::CreateDecoder( const InputPtr& _input )
    {
        NANE_ASSERT_RV( m->interface != NULL, "SoundCodecSystem must be initialized", NullPtr );
        NANE_ASSERT_RV( _input != NullPtr, "Input must be valid", NullPtr );

        SoundDecoder::Implementation* soundDecoderImpl = NULL;
        ISoundDecoder* soundDecoderInterface = m->interface->CreateDecoder(_input.Get());
        if( soundDecoderInterface != NULL )
        {
            soundDecoderImpl = new SoundDecoder::Implementation(m, soundDecoderInterface);
        }
        return SoundDecoder(soundDecoderImpl);
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
    SoundDecoder SoundCodecSystem::CreateDecoder( const InputSeekablePtr& _inputSeekable )
    {
        NANE_ASSERT_RV( m->interface != NULL, "SoundCodecSystem must be initialized", NullPtr );
        NANE_ASSERT_RV( _inputSeekable != NullPtr, "Input must be valid", NullPtr );

        SoundDecoder::Implementation* soundDecoderImpl = NULL;
        ISoundDecoder* soundDecoderInterface = m->interface->CreateDecoder(_inputSeekable.Get());
        if( soundDecoderInterface != NULL )
        {
            soundDecoderImpl = new SoundDecoder::Implementation(m, soundDecoderInterface);
        }
        return SoundDecoder(soundDecoderImpl);
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
    SoundDecoder SoundCodecSystem::CreateDecoder( const ReadFilePtr& _file )
    {
        return CreateDecoder(static_cast<const InputSeekablePtr&>(_file));
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
}   // namespace nane
